//
//  ShopToolView.m
//  danpin
//
//  Created by chuangjia on 22/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "ShopToolView.h"
#import "viewconfig.h"

#import "KWPopoverView.h"


@interface ShopToolView()
{
    
}

@property(nonatomic,strong)UILabel *alertLabel;//提醒用户选择尺寸和颜色
@property(nonatomic,strong)UILabel *taxLabel; //税费
@property(nonatomic,strong)UILabel *priceLabel; //总价
@property(nonatomic,strong)UILabel *fareLabel; //运费
@property(nonatomic,strong) UILabel *qtyLabel; //数量

@property(strong,nonatomic)UIView *numView;//购买商品的数量
@property(strong,nonatomic)UILabel *numCountLab;//购买商品的数量
@property(strong,nonatomic) UIButton *deleteBtn;
@property(strong,nonatomic) UIButton *addBtn;



@end
@implementation ShopToolView
@synthesize priceLabel=_priceLabel;
@synthesize fareLabel=_fareLabel;

@synthesize priceStr=_priceStr;
@synthesize fareStr=_fareStr;
@synthesize taxStr=_taxStr;
@synthesize qtyStr = _qtyStr;
@synthesize numView=_numView;
@synthesize numCountLab=_numCountLab;
@synthesize numViewCount=_numViewCount;


-(void)popover:(UITapGestureRecognizer *)tap
{
    //the controller we want to present as a popover
    UILabel* btn = (UILabel*)tap.view;
    
    float fHeight = 40;// (self.frame.size.height)/2;
    float fWidth = fHeight*3;
    
    CGPoint pt = CGPointMake(btn.frame.origin.x + btn.frame.size.width/2 - fWidth/2,
                             self.superview.frame.size.height - self.frame.size.height - fHeight);

    

    
    _numView.frame = CGRectMake(pt.x,
                                pt.y,
                                fWidth,
                                fHeight);
    
    _deleteBtn.frame = CGRectMake(0,0,fHeight,fHeight);
    _numCountLab.frame = CGRectMake(fHeight,0,fHeight,fHeight);
    _addBtn.frame = CGRectMake(fHeight*2,0,fHeight,fHeight);
    
    
    pt = CGPointMake(btn.frame.origin.x + btn.frame.size.width/2,
                     self.superview.frame.size.height - btn.center.y);

    [KWPopoverView showPopoverAtPoint:pt inView:self.superview withContentView:_numView label:btn];
}




-(void) showQtySelector:(id)sender
{
    if(self.enableQtySelect)
        [self popover:sender];
}
/*
- (void)drawRect:(CGRect)rect
{
    //画线
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context,0.5);//线宽度
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.4);//线条颜色
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, self.frame.size.width, 0);
    CGContextStrokePath(context);
}
*/
- (id)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.enableQtySelect = YES;
        //  背景色黑色半透明
        self.backgroundColor = DARK_BACKGROUND_COLOR;//[UIColor blackColor];
        [self initUI];
        
    }
    return self;
    
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.priceStr=nil;
    self.fareStr=nil;
    [self.priceLabel removeFromSuperview];
    self.priceLabel=nil;
    
    [self.fareLabel removeFromSuperview];
    self.fareLabel=nil;
    
    
    [self.alertLabel removeFromSuperview];
    self.alertLabel=nil;
    
    [self.taxLabel removeFromSuperview];
    self.taxLabel=nil;
    [self.numView removeFromSuperview];
    self.numView=nil;
    
}
-(void)setCallBack:(void(^)(ShopToolEventType,UIView *,id,id))callback
{
    self.actionBlock =callback;
}

-(void)initUI
{
    //到手价
    _priceLabel =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_TIME_PADDING, 200, (self.frame.size.height)/2)];
    _priceLabel.backgroundColor = [UIColor clearColor];
    _priceLabel.textColor = PRICE_LABEL_COLOR;//UIColorFromRGB(0xffffff);
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    _priceLabel.font = FONT(14);
    _priceLabel.text = @"";
    [self addSubview:_priceLabel];
    
    
    //税费
    _qtyLabel =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,
                                                          (self.frame.size.height)/2 - 14,
                                                          100,
                                                          14)];
    _qtyLabel.backgroundColor = [UIColor clearColor];
    _qtyLabel.textColor = [UIColor darkGrayColor];//UIColorFromRGB(0xffffff);
    _qtyLabel.textAlignment = NSTextAlignmentLeft;
    _qtyLabel.font = FONT(10);
    _qtyLabel.text = @"";
    _qtyLabel.userInteractionEnabled = YES;
  //  [_qtyLabel addTarget:self action:@selector(showQtySelector:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_qtyLabel];
    
    UITapGestureRecognizer *tapGestureTel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showQtySelector:)];
    [_qtyLabel addGestureRecognizer:tapGestureTel];
    tapGestureTel=nil;
    
    
    //提醒用户选择尺寸颜色
    _alertLabel =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,
                                                           (self.frame.size.height)/2 - PHOTO_FRAME_WIDTH,
                                                           200,
                                                           (self.frame.size.height)/2)];
    _alertLabel.backgroundColor = [UIColor clearColor];
    _alertLabel.textColor = [UIColor darkGrayColor];//UIColorFromRGB(0xffffff);
    _alertLabel.textAlignment = NSTextAlignmentLeft;
    _alertLabel.font = FONT(16);
    _alertLabel.text = @"";
    [self addSubview:_alertLabel];
    
    
    //税费
    _taxLabel =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,
                                                         (self.frame.size.height)/2 + PHOTO_TIME_PADDING,
                                                         200,
                                                         (self.frame.size.height)/2)];
    _taxLabel.backgroundColor = [UIColor clearColor];
    _taxLabel.textColor = [UIColor grayColor];//UIColorFromRGB(0xffffff);
    _taxLabel.textAlignment = NSTextAlignmentLeft;
    _taxLabel.font = FONT(10);
    _taxLabel.text = @"";
    [self addSubview:_taxLabel];
    
    
    //运费
    _fareLabel =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, (self.frame.size.height)/2 + PHOTO_TIME_PADDING, 200, (self.frame.size.height)/2)];
    _fareLabel.backgroundColor = [UIColor clearColor];
    _fareLabel.textColor = [UIColor grayColor];//UIColorFromRGB(0xffffff);
    _fareLabel.textAlignment = NSTextAlignmentLeft;
    _fareLabel.font = FONT(10);
    _fareLabel.text = @"";
    [self addSubview:_fareLabel];
    
    
    _buyBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _buyBtn.frame =CGRectMake(self.frame.size.width -widthAuto(100) ,0, widthAuto(100), self.frame.size.height);
    _buyBtn.backgroundColor= [UIColor grayColor];//RED_COLOR;
    _buyBtn.tag=0;
    _buyBtn.titleLabel.font = FONT_BOLD(14);//(14);
    [_buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_buyBtn setTitle:@"一键海淘" forState:UIControlStateNormal];
    [_buyBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_buyBtn];
    
    
    
    
    float fHeight = (self.frame.size.height)/3;
    
#define WHITE_GRAY_COLOR [UIColor colorWithRed:0.637 green:0.637 blue:0.737 alpha:1]
#define DARK_BUTTON_BACKGROUND  [UIColor colorWithRed:0.26 green:0.26 blue:0.26 alpha:1]
#define DISABLED_BUTTON_BACKGROUND  [UIColor colorWithRed:0.16 green:0.16 blue:0.16 alpha:1]    
    
    _numView = [[UIView alloc] initWithFrame:CGRectMake(_buyBtn.frame.origin.x - 100, (self.frame.size.height-fHeight)/2, fHeight*3, fHeight)];
    _numView.backgroundColor =[UIColor clearColor];
    _numView.hidden = YES;
//    [self addSubview:_numView];
  
    //减按钮
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn.backgroundColor = DARK_BUTTON_BACKGROUND;
    _deleteBtn.titleLabel.font = FONT(12);
    _deleteBtn.titleLabel.textColor = WHITE_GRAY_COLOR;
    [_deleteBtn setTitleColor:WHITE_GRAY_COLOR forState:UIControlStateNormal];
    [_deleteBtn setTitleColor:DISABLED_BUTTON_BACKGROUND forState:UIControlStateDisabled];
    
    _deleteBtn.frame = CGRectMake(0, 0, fHeight, fHeight);
    _deleteBtn.layer.borderWidth=0.5;
    _deleteBtn.layer.borderColor =WHITE_GRAY_COLOR.CGColor;
    [_deleteBtn setTitle:@"-" forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    _deleteBtn.tag = 11;
    [_numView addSubview:_deleteBtn];
    
    //购买商品的数量
    _numCountLab = [[UILabel alloc]initWithFrame:CGRectMake(fHeight, 0, fHeight, fHeight)];
    _numCountLab.backgroundColor = DARK_BUTTON_BACKGROUND;
    _numCountLab.textAlignment = NSTextAlignmentCenter;
    _numCountLab.font=FONT(12);
    _numCountLab.textColor = WHITE_GRAY_COLOR;
    _numCountLab.layer.borderWidth=0.5;
    _numCountLab.layer.borderColor =WHITE_GRAY_COLOR.CGColor;
    _numCountLab.text = @"1";
    [_numView addSubview:_numCountLab];
    
    //加按钮
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addBtn.backgroundColor = DARK_BUTTON_BACKGROUND;
    _addBtn.frame = CGRectMake(fHeight*2, 0, fHeight, fHeight);
    _addBtn.titleLabel.font = FONT(12);
    _addBtn.layer.borderWidth=0.5;
    _addBtn.layer.borderColor =WHITE_GRAY_COLOR.CGColor;
    [_addBtn setTitleColor:WHITE_GRAY_COLOR forState:UIControlStateNormal];
    [_addBtn setTitleColor:DISABLED_BUTTON_BACKGROUND forState:UIControlStateDisabled];
    
    [_addBtn setTitle:@"+" forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    _addBtn.tag = 10;
    [_numView addSubview:_addBtn];
    
    

    
}


- (void) EnableBuyButton:(BOOL) yesNo
{
    if(yesNo)
    {
        _buyBtn.backgroundColor = UIColorFromRGB(0xDE3B30);
      //   _numView.hidden = NO;
       
        
    }
    else
    {
        _buyBtn.backgroundColor = [UIColor darkGrayColor];
        _numView.hidden = YES;
        
        _fareLabel.text = @"";
        [_fareLabel sizeToFit];//让税费靠到左边来，避免出现空白区
        _priceLabel.text = @"";
        _qtyLabel.text = @"";
        _taxLabel.text = @"";
        _alertLabel.text = @"";
    }
    
}
-(void)clickEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    ShopToolEventType type=ShopToolEventType_unknow;
    switch (btn.tag) {
        case 0:
        {
            type =ShopToolEventType_buy;
        }
            break;
        case 10:
        {
            type =ShopToolEventType_add;
        }
            break;
        case 11:
        {
            type =ShopToolEventType_del;
        }
            break;
        default:
            break;
    }
    if (self.actionBlock)
    {
        self.actionBlock(type,self,nil,nil);
    }
}
//
-(void)setPriceStr:(NSString *)priceStr
{
    _priceStr=priceStr;
    if (priceStr.length>0)
    {
        self.priceLabel.text  =[NSString stringWithFormat:@"到手价: ¥%@",priceStr];
        
        
        [self.priceLabel sizeToFit];
        
        [self setAlertStr:@""];
    
    }
    else
    {
        self.priceLabel.text  = priceStr;
    }
}
-(void)setFareStr:(NSString *)fareStr
{
    _fareStr=fareStr;
    if (fareStr.length>0)
    {
        self.fareLabel.text  =[NSString stringWithFormat:@"运费: ¥%@",fareStr];
        
        [self.fareLabel sizeToFit];
    }
}

-(void)setTaxStr:(NSString *)taxStr
{
    _taxStr=taxStr;
    if (taxStr.length>0)
    {
        self.taxLabel.text  =[NSString stringWithFormat:@"预收税: ¥%@",taxStr];
        
        
        float fLeft = self.fareLabel.frame.origin.x + self.fareLabel.frame.size.width + PHOTO_FRAME_WIDTH;
        
        if([self.fareLabel.text length] == 0)
            fLeft = self.fareLabel.frame.origin.x;
        
        self.taxLabel.frame = CGRectMake(fLeft,
                                         self.taxLabel.frame.origin.y, 100, self.taxLabel.frame.size.height);
        
        [self.taxLabel sizeToFit];
    }
}

-(void)setNumViewCount:(NSString *)numViewCount maxQty:(int)nMaxQty
{
  //  return;
    
    _numViewCount=numViewCount;
    if (nMaxQty>1 && [_numViewCount intValue] > 0)
    {
        self.numCountLab.text  = numViewCount;
    //    _numView.hidden = NO;
    }

    
    _addBtn.backgroundColor = DARK_BUTTON_BACKGROUND;
    _deleteBtn.backgroundColor = DARK_BUTTON_BACKGROUND;
 
    if([numViewCount intValue] == 1)
    {
        _deleteBtn.backgroundColor = DISABLED_BUTTON_BACKGROUND;
 //      [_deleteBtn setEnabled:NO];
    }
    else if([numViewCount intValue] == nMaxQty)
    {
        _addBtn.backgroundColor = DISABLED_BUTTON_BACKGROUND;
  //      [_addBtn setEnabled:NO];
    }
    
    
}

-(void)setQtyStr:(NSString *)qtyStr
{
    _qtyStr=qtyStr;
    if (_qtyStr.length>0)
    {
        self.qtyLabel.text  = _qtyStr;//[NSString stringWithFormat:@"(%@ x %@)",priceStr,_qtyStr];
        
        self.qtyLabel.frame = CGRectMake(self.priceLabel.frame.origin.x + self.priceLabel.frame.size.width + PHOTO_FRAME_WIDTH,
                                         self.qtyLabel.frame.origin.y,
                                         60,
                                         self.qtyLabel.frame.size.height);
        
        [self.qtyLabel sizeToFit];
    }
    else
    {
        self.numCountLab.text = @"";
    }
}



-(void)setQtyStr:(NSString *)qtyStr price:(NSString*)priceStr
{
    _qtyStr=qtyStr;
    
    if (_qtyStr.length > 0)
    {
        self.qtyLabel.text  =[NSString stringWithFormat:@"(%@ x %@)",priceStr,_qtyStr];
        
        self.qtyLabel.frame = CGRectMake(self.priceLabel.frame.origin.x + self.priceLabel.frame.size.width + PHOTO_FRAME_WIDTH,
                                         self.qtyLabel.frame.origin.y,
                                         60,
                                         self.qtyLabel.frame.size.height);
        
        [self.qtyLabel sizeToFit];
    }
    else
    {
        self.qtyLabel.text = @"";
    }
}



-(void)setAlertStr:(NSString *)alertStr
{
    self.alertLabel.text  =  alertStr;//@"请先选择尺寸/颜色";//[NSString stringWithFormat:@"预收税: ¥%@",taxStr];
    
    [self.alertLabel sizeToFit];
}




@end
